package com.example.controller;

import com.example.entity.User;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

@RestController
@RequestMapping("test")
public class TestController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @RequestMapping("{k}/{v}")
    public String test(@PathVariable("k") String key, @PathVariable("v") String value) {
        System.out.println("K: " + key + "\t\tV: " + value);

        /**
         * 使用 redis 设值
         */
        redisTemplate.opsForValue().set(key, value);
        return "success";
    }

    @RequestMapping("/user/{name}")
    public String testUser(@PathVariable("name") String name) {
        System.out.println("name: " + name);

        String key = "user:id:" + (int) (Math.random() * (100 - 0 + 1));
        User user = new User();
        user.setUserName(name);
        user.setUserAge((int) (Math.random() * (100 - 0 + 1)));
        /**
         * Value 是一个对象
         */
        redisTemplate.opsForValue().set(key, user);
        return "success";
    }
}
